﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{

    [TestFixture]
    public class TestDMToClassDefsXmlConverter
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        [Test]
        public void Test_ConvertToXml_DMSolution_XmlIsWellFormed()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();

            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            DMToClassDefsXmlConverter converter = new DMToClassDefsXmlConverter();
            string xml = converter.ConvertToXml((DMSolution) dmClass.Solution);

            //---------------Test Result -----------------------
            XmlDocument doc = new XmlDocument();
            doc.Load(new StringReader(xml));
        }


        [Test]
        public void Test_ConvertToXml_DMClass_XmlIsWellFormed()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            DMToClassDefsXmlConverter converter = new DMToClassDefsXmlConverter();
            string xml = converter.ConvertToXml(dmClass);
            //---------------Test Result -----------------------
            XmlDocument doc = new XmlDocument();
            doc.Load(new StringReader(xml));
        }

        [Test]
        public void Test_ConvertToXml_UIView_XmlIsWellFormed()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtilsShared.CreateUnsavedValidUIInfo();
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            DMToClassDefsXmlConverter converter = new DMToClassDefsXmlConverter();
            string xml = converter.ConvertToXml(uiView);
            //---------------Test Result -----------------------
            XmlDocument doc = new XmlDocument();
            doc.Load(new StringReader(xml));
        }

        [Test]
        public void Test_ConvertToXml_CreatesValidClassDefs()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            int loadedClassDefCount = ClassDef.ClassDefs.Count;
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            DMToClassDefsXmlConverter converter = new DMToClassDefsXmlConverter();
            string xml = converter.ConvertToXml((DMSolution) dmClass.Solution);
            //---------------Test Result -----------------------
            XmlClassDefsLoader xmlClassDefsLoader = new XmlClassDefsLoader(xml, new DtdLoader());
            ClassDefCol classDefs = xmlClassDefsLoader.LoadClassDefs();
            Assert.AreEqual(loadedClassDefCount, ClassDef.ClassDefs.Count, "Must not add to test fixture's class defs");

            Assert.AreEqual(1, classDefs.Count);
            Assert.IsTrue(classDefs.Contains(dmClass.Assembly.Name, dmClass.ClassNameBO));
        }

        [Ignore] //TODO Eric 31 Aug 2009: need to implement generated and get the defaults set to true
        [Test]
        public void Test_ConvertToXml_DoesNotGenerateProperties_GeneratedSetToFalse()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassTwoProperties();
            DMProperty firstProperty = dmClass.Properties[0];
            DMProperty secondProperty = dmClass.Properties[1];
            firstProperty.Generated = true;
            secondProperty.Generated = false;
            //---------------Assert Precondition----------------
            Assert.IsTrue(firstProperty.Generated.Value);
            Assert.IsFalse(secondProperty.Generated.Value);
            //---------------Execute Test ----------------------
            DMToClassDefsXmlConverter converter = new DMToClassDefsXmlConverter();
            string xml = converter.ConvertToXml((DMSolution) dmClass.Solution);
            //---------------Test Result -----------------------
            XmlClassDefsLoader xmlClassDefsLoader = new XmlClassDefsLoader(xml, new DtdLoader());
            ClassDefCol classDefs = xmlClassDefsLoader.LoadClassDefs();

            Assert.AreEqual(1, classDefs.Count);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];
            Assert.AreEqual(1, classDef.PropDefcol.Count);
            Assert.IsTrue(classDef.PropDefcol.Contains(firstProperty.PropertyName));
        }
    }

   
}
